<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <table class="table table-hover">
        </table>
    </div>
    <p></p>

    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">编辑笔记</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <input type="hidden" name="id" id="noteid">
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">标题:</label>
                            <input type="text" class="form-control" id="updatenotetitle">
                        </div>
                        <div class="form-group">
                            <label for="message-text" class="control-label">内容:</label>
                            <textarea class="form-control" id="message-text"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" onclick="update()">Send message</button>
                </div>
            </div>
        </div>
    </div>

    <script src="./js/request.js"></script>
    <script>
        let updateNoteTitle = document.querySelector('#updatenotetitle');
        let messageText = document.querySelector('#message-text');
        let noteId = document.querySelector('#noteid');

        noteAxios('/noteList?page=0&limit=8').then(({
            data,
            code,
            message
        }) => {
            // console.log(val);

            if (code === 2000) {
                let str = data.map(item => `
                    <tr>
                        <td>${item.title}</td>
                        <td><a  data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" onclick="showupdate('${item._id}')">编辑</a></td>
                    </tr>

                
                `).join('');

                document.querySelector('.table').innerHTML = str
            } else {
                document.querySelector('p').innerHTML = message;
            }
        })


        function update() {
            // console.log("修改");
            noteAxios({
                url: "/updateNote",
                method: "post",
                data: {
                    id: noteId.value,
                    content: messageText.value,
                    title: updateNoteTitle.value
                }
            }).then(val => {
                console.log(val);
                if (val.code === 2000) {
                    $('#exampleModal').modal('hide');
                    window.location.reload();
                }

            })
        }

        async function showupdate(id) {
            // console.log("显示");

            let {
                data
            } = await noteAxios(`/detailNote?id=${id}`);

            console.log(data);
            updateNoteTitle.value = data.title;
            messageText.value = data.content;
            noteId.value = data._id;
        }
    </script>
</body>

</html>